package com.android.clinic.app.ui.widget;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;

import com.android.clinic.app.ui.widget.dialog.CustomerAlertDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheng on 2018/2/8.
 */
public class UIAlertView {

    private static CustomerAlertDialog dialog = null;
    private static OnThemeDialogListener listener;

    private static void setOnDismissUpdateListener(OnThemeDialogListener onThemeDialogListener) {
        listener = onThemeDialogListener;
    }

    public interface OnThemeDialogListener {
        void OnDismissButton();

        void OnSureButton();
    }

    /**
     * 支付成功
     * @param context
     * @param onDismissUpdateListener
     */
    public static void paySuccessDialog(final Context context, final OnThemeDialogListener onDismissUpdateListener) {
        dialog = new CustomerAlertDialog(context, 1);
        dialog.setCancelable(false);
        dialog.setTitle("支付成功");
        dialog.setContent("");
        dialog.setSubmitName("确定");
        dialog.setOnClickListener((v, type) -> {
            if (type == CustomerAlertDialog.ClickType.CONFIRM) {
                onDismissUpdateListener.OnSureButton();
            }
        });
        dialog.show();
    }

    /**
     * 拨打电话
     *
     * @param context
     * @param title
     * @param phoneNum
     */
    public static void callPhoneDialog(final Context context, String title, final String phoneNum) {
        dialog = new CustomerAlertDialog(context);
        dialog.setTitle(title);
        dialog.setContent(phoneNum);
        dialog.setOnClickListener((v, type) -> {
            if (type == CustomerAlertDialog.ClickType.CONFIRM) {
                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNum));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });
        dialog.show();
    }

    /**
     * 弹出框
     *
     * @param context
     */
    public static void alertDialog(final Context context, final String title, final String content, final OnThemeDialogListener onDismissUpdateListener, String submitName, String cancelName) {
        setOnDismissUpdateListener(onDismissUpdateListener);
        dialog = new CustomerAlertDialog(context);
        dialog.setCancelable(false);
        dialog.setTitle(title);
        dialog.setContent(content);
        dialog.setSubmitName(submitName);
        dialog.setCancelName(cancelName);
        dialog.setOnClickListener((v, type) -> {
            if (onDismissUpdateListener != null) {
                if (type == CustomerAlertDialog.ClickType.CONFIRM) {
                    onDismissUpdateListener.OnSureButton();
                } else if (type == CustomerAlertDialog.ClickType.CANCEL) {
                    onDismissUpdateListener.OnDismissButton();
                }
            }

        });
        dialog.show();
    }

    /**
     * 检查手机上是否安装了指定的软件
     *
     * @param context
     * @param packageName ：应用包名
     * @return
     */
    public static boolean isAvailable(Context context, String packageName) {
        // PackageManager
        final PackageManager packageManager = context.getPackageManager();
        // 获取所有已安装程序的包信息
        List<PackageInfo> packageInfo = packageManager.getInstalledPackages(0);
        // 用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        // 从packageInfo中将包名字逐一取出，压入pName list中
        if (packageInfo != null) {
            for (int i = 0; i < packageInfo.size(); i++) {
                String packName = packageInfo.get(i).packageName;
                packageNames.add(packName);
            }
        }
        // 判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }

}
